package com.maxd.service.selfUser;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.Goods;
import com.maxd.model.SelfUserBrowse;
import com.maxd.modules.selfshop.merchants.respository.SelfStoreJpaRepository;
import com.maxd.respository.selfGoods.GoodsJpaRepository;
import com.maxd.respository.selfUser.SelfUserBrowseJpaRepository;
import com.maxd.service.selfGoods.GoodsService;
import com.maxd.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class SelUserBrowseServiceImpl implements SelfUserBrowseService {
    @Autowired
    private SelfUserBrowseJpaRepository jpaRepository;
    @Autowired
    private GoodsJpaRepository goodsJpaRepository;
    @Autowired
    private SelfStoreJpaRepository selfStoreJpaRepository;

    @Override
    public Result findAll(Integer page, Integer size, Long userId, String createTime) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime")));
        Specification<SelfUserBrowse> queryCondition = new Specification<SelfUserBrowse>() {
            @Override
            public Predicate toPredicate(Root<SelfUserBrowse> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (userId != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("userId"), userId));
                }
                if (StringUtils.isNotEmpty(createTime)) {
                    predicateList.add(criteriaBuilder.like(root.get("createTime"), "%"+createTime+"%"));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Page<SelfUserBrowse> all = jpaRepository.findAll(queryCondition, pageable);
        for (SelfUserBrowse s : all.getContent()) {
            if(s.getGoodsId()!=null){
                s.setGoods(goodsJpaRepository.findById(s.getGoodsId()).orElse(null));
                if(s.getGoods()!=null && s.getGoods().getStoreId()!=null){
                    s.getGoods().setStore(selfStoreJpaRepository.findById(s.getGoods().getStoreId()).orElse(null)); //店铺信息
                }
            }
        }
        return ResultUtil.success(all);
    }

    @Override
    public Result saveBody(SelfUserBrowse entity) {
        goodsJpaRepository.browse(entity.getGoodsId());
        //查重
        SelfUserBrowse s = jpaRepository.findByGoodsIdAndUserId(entity.getGoodsId(), entity.getUserId());
        if (s != null && s.getId() != null){
            s.setCreateTime(DateUtil.createTime()); //更新时间
            return ResultUtil.success(jpaRepository.save(s));
        }
        entity.setCreateTime(DateUtil.createTime()); //创建时间
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result updateBody(SelfUserBrowse entity) {
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result findOne(Long id) {
        return ResultUtil.success(jpaRepository.findById(id).orElse(null));
    }

    @Override
    public Result delete(Long id) {
        jpaRepository.deleteById(id);
        return ResultUtil.success();
    }

}
